//
//  PhotoBrowserAnimation.swift
//  SinaWeiBoProject
//
//  Created by 房城鸿 on 15/10/22.
//  Copyright © 2015年 房兰峰. All rights reserved.
//

import UIKit
import SDWebImage

class PhotoBrowserAnimation: NSObject ,UIViewControllerTransitioningDelegate{

    //是否展现的标记
    var isPressented = false
    
    //起始位置
    var fromRect = CGRectZero
    //目标位置
    var toRect = CGRectZero
    //图像视图的URL 
    var url:NSURL?
    //动画播放的图像视图
    lazy var imageView:HMProgressImageView = {
        let iv = HMProgressImageView()
        iv.contentMode = UIViewContentMode.ScaleAspectFill
        iv.clipsToBounds = true
        return iv
    }()
    
    ///  准备动画参数
    ///
    ///  - parameter formRect: formRect
    ///  - parameter toRect:   toRect
    ///  - parameter url:      url
    func prepareAnimation(fromRect:CGRect,toRect:CGRect,url:NSURL){
        self.fromRect = fromRect
        self.toRect = toRect
        self.url = url
    }
    
    //返回提供展现转场动画的对象
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPressented = true
        return self
    }
    
    ///  返回 提供解除 deimiss转场动画的对象
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPressented = false
        return self
    }
}

//MARK: - UIViewControllerAnimatedTransitioning - 转场动画协议 - 专门提供转场动画的细节
extension PhotoBrowserAnimation: UIViewControllerAnimatedTransitioning {

    // 返回转场动画时长
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }
    ///  专门实现转场动画效果  -- 一点实现了此方法， 程序员必须完成动画效果
    ///
    ///  - parameter transitionContext: transition [转场]Context 【上下文】 提供了转场动画所需的一切细节
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
        let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
        
        printLog(fromVC)
        printLog(toVC)
        isPressented ? presentAnim(transitionContext) : dismissAnim(transitionContext)
    }
    
    ///  实现解除转场动画
    ///
    ///  - parameter transitionContext: 上下文
    private func dismissAnim(transitionContext:UIViewControllerContextTransitioning){
    
        //拿到 被展现的视图 fromVc
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            
            fromView.frame = self.fromRect
            
            }) { (_) -> Void in
                //将视图从容器视图中移除
                fromView.removeFromSuperview()
                //动画完成
                transitionContext.completeTransition(true)
        }
    }
    
    ///  实现Modal  展现动画
    ///
    ///  - parameter transitionContext: transitionContext
    private func presentAnim(transitionContext:UIViewControllerContextTransitioning){
    
        //1.将iamgeView 添加到容器视图
        
        transitionContext.containerView()?.addSubview(imageView)
        imageView.frame = fromRect
        
        //2. 用sdwebimage 异步下载图像
        /**
        sd_setImageWithURL
        1. 如果图片已经被缓存，不会再次下载
        2. 如果要跟进进度，都是 “异步” 回调
            原因：
            A.一般程序不会跟进进度
            B.进度回调的频率相对较高
            异步回调，弄呢狗狗降低对主线程的压力
        */
        imageView.sd_setImageWithURL(url, placeholderImage: nil, options: [SDWebImageOptions.RetryFailed], progress: { (current, total) -> Void in
            
            print("\(current) \(total)\(NSThread.currentThread())")
            //设置进度 - 计算下载进度
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.imageView.progress = CGFloat(current) / CGFloat(total)
                
            })
            
            }) { (_, error, _, _) -> Void in
                //判断是否有误
                if error != nil{
                    printLog(error)
                    //声明动画结束 - 参数为false  容器视图不会添加，转场失败
                    transitionContext.completeTransition(false)
                    return
                }
                
                //3. 图像下载完成之后，在显示动画
                UIView.animateWithDuration(self.transitionDuration(transitionContext), animations: { () -> Void in
                    self.imageView.frame = self.toRect
                    
                    }, completion: { (_) -> Void in
                        
                        //将目标视图添加到容器视图中
                        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
                        transitionContext.containerView()?.addSubview(toView)
                        
                        //将 iamgeView 从界面上删除
                        self.imageView.removeFromSuperview()
                        
                        //声明动画完成
                        transitionContext.completeTransition(true)
                })
                
        }
        
    }
}




















